package partner_system_api

import (
	"gitee.com/theegg/go-brick/app/controller/partner_api"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_permissions"
	"gitee.com/theegg/go-brick/app/model/system/system_images"
	"gitee.com/theegg/go-brick/app/model/system/system_modules"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/cstruct"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

// @title SystemModuleList
// @description 获取管理角色列表
func SystemModuleList(c *gin.Context) {
	// 检验参数
	var validaParams system_modules.QueryParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	// 获取数据
	data, err := system_modules.GetDataList(cstruct.StructToMap(validaParams))

	if err != nil {
		app.NewResponse(c).JsonReturn(false, "获取数据失败", nil)
		return
	}

	returnData := make(map[string]interface{})

	returnData["module_type"] = system_modules.ConfigureType
	returnData["data"] = data

	app.NewResponse(c).JsonReturn(true, "获取数据成功", returnData)
	return
}

// @title SystemModuleAdd
// @description 添加或者更新配置
func SystemModuleAdd(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	var modelPc *system_modules.SystemModules
	if id > 0 {
		modelPc, err = system_modules.GetItemById(id)
		if err != nil {
			app.NewResponse(c).JsonReturn(false, err.Error(), nil)
			return
		}
	} else {
		modelPc = new(system_modules.SystemModules)
	}

	// 校验参数
	var validaParams system_modules.AddParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, "bind-json-"+err.Error(), nil)
		return
	}

	adminConfigure, err := modelPc.AddOrUpdateSystemModule(validaParams)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), adminConfigure)
		return
	}

	app.NewResponse(c).JsonReturn(true, "添加数据成功", adminConfigure)
	return
}

// @title SystemModuleDetail
// @description 编辑配置
func SystemModuleDetail(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	item, errItem := system_modules.GetItemById(id)
	if errItem != nil {
		app.NewResponse(c).JsonReturn(false, errItem.Error(), nil)
		return
	}

	data := make(map[string]interface{})
	detail := make(map[string]interface{})
	detail["id"] = item.Id
	detail["title"] = item.Title
	detail["sign"] = item.Sign
	detail["mark"] = item.Mark
	detail["module_type"] = item.ModuleType
	detail["node_list"] = strings.Split(item.NodeList, ",")

	imageListArr := make([]map[string]interface{}, 0)
	if item.ImageList != "" {
		for ii, vv := range strings.Split(item.ImageList, ",") {
			if vv != "" {
				imageListArr = append(imageListArr, map[string]interface{}{
					"id":         ii,
					"image_name": vv,
					"url":        app.GetImageStaticUrl(vv),
				})
			}
		}
	}

	detail["image_list_arr"] = imageListArr

	detail["link_list"] = strings.Split(item.LinkList, ",")
	detail["image_list"] = strings.Split(item.ImageList, ",")

	data["item"] = detail
	data["module_type_option"] = system_modules.ConfigureType
	app.NewResponse(c).JsonReturn(true, "获取数据成功", data)
	return
}

// @title SystemModuleDel
// @description 删除配置
func SystemModuleDel(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	// 判定当前登陆用户是否存在
	currentAdminUser := partner_api.GetLoginPartnerAdminUser(c)

	// 只有超级管理员才可以
	role, err := currentAdminUser.Role()
	if err != nil {
		app.NewResponse(c).JsonReturn(false, app.NewError(err).Error(), nil)
		return
	}

	if !role.IsSuperRole() {
		app.NewResponse(c).JsonReturn(false, app.NewError("对不起,只有超级管理员可以删除配置!").Error(), nil)
		return
	}

	// 删除
	ok, err := partner_admin_permissions.DeleteAdminPermission(id)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	if !ok {
		app.NewResponse(c).JsonReturn(false, "对不起，删除数据发生未知错误,请联系客服!", nil)
		return
	}

	app.NewResponse(c).JsonReturn(true, "删除成功成功", nil)
	return
}

// @title NodeImageUpload
// @description 图片上传
func SystemModuleImageUpload(c *gin.Context) {

	imageName, errUpload := system_images.SaveImage("system_module", "list", c)
	if errUpload != nil {
		app.NewResponse(c).JsonReturn(false, errUpload.Error(), nil)
		return
	}

	returnData := make(map[string]interface{})
	returnData["file_url"] = app.GetImageStaticUrl(imageName)
	returnData["file_name"] = imageName

	app.NewResponse(c).JsonReturn(true, "上传图片成功!", returnData)
	return
}
